# -*- coding: utf-8 -*-
"""
Created on Thu Dec 14 13:07:37 2017

@author: Luther
"""

import requests
import pandas as pd

path_input = input("请输入文件路径:")
codes = pd.read_excel(path_input, convert_float=False)
codes = codes.drop(0)

results = pd.DataFrame()
log = pd.DataFrame()
code = []
name = []
title = []
ajurl = []
log_code = []
log_page = []


def get_url(stock, column, num):
    url = "http://www.cninfo.com.cn/cninfo-new/announcement/query?stock={0:}&searchkey=&category=category_scgkfx_szsh%3B&pageNum={2:}&pageSize=30&column={1:}&tabName=fulltext&sortName=&sortType=&limit=&seDate=".format(
        stock, column, num)
    return url


def get_content(d):
    content = []
    content.append(d['secCode'])
    content.append(d['secName'])
    content.append(d['announcementTitle'])
    aj = "http://www.cninfo.com.cn/" + d['adjunctUrl']
    content.append(aj)
    return content


count = 0
for i in range(len(codes)):

    stock = str(codes.stock[i + 1]).split(".")[0]
    column = codes.column[i + 1]
    url = get_url(stock, column, num=1)

    try:
        r = requests.post(url, timeout=60)
        r.raise_for_status
    except:
        log_code.append(stock)
        log_page.append(404)
    if r.status_code == 200:
        n = r.json()["totalRecordNum"]
        if n > 0:
            pages = n // 30 + 1
            l = r.json()['announcements']
            for x in range(len(l)):
                d = l[x]
                content = get_content(d)
                code.append(content[0])
                name.append(content[1])
                title.append(content[2])
                ajurl.append(content[3])
            log_code.append(stock)
            log_page.append(1)
            if pages > 1:
                for page in range(2, pages + 1):
                    url = get_url(stock, column, page)
                    try:
                        r = requests.post(url, timeout=60)
                        r.raise_for_status
                        l = r.json()['announcements']
                        for x in range(len(l)):
                            d = l[x]
                            content = get_content(d)
                            code.append(content[0])
                            name.append(content[1])
                            title.append(content[2])
                            ajurl.append(content[3])
                        log_code.append(stock)
                        log_page.append(page)
                    except:
                        log_code.append(stock)
                        log_page.append(-page)
        else:
            log_code.append(stock)
            log_page.append(0)
    count = count + 1
    print("\r当前进度: {:.2f}%".format(count * 100 / len(codes)), end="")

results['code'] = code
results['name'] = name
results['title'] = title
results['url'] = ajurl

log['log_code'] = log_code
log['log_page'] = log_page

path_output = input("\n请输入输出目录:")
path_results = path_output + "\\results.csv"
results.to_csv(path_results)
path_log = path_output + "\\log.csv"
log.to_csv(path_log)
